package kr.or.kead.ui.meun;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import kr.or.kead.service.DaoCourse;
import kr.or.kead.service.DaoProfessor;
import kr.or.kead.service.DaoTable;
import kr.or.kead.ui.list.AbsTableList;

public abstract class AbsMeun extends JMenu implements ActionListener {
	protected JMenuItem delMenu;
	protected JMenuItem listMenu;
	protected JMenuItem updateMenu;
	protected JMenuItem addMenu;
	protected JFrame frame;
	
	public AbsMeun(JFrame frame, String list) {
		this.frame = frame;
		initialize(list);
	}
	private void initialize(String list) {
		setText(list);
		addMenu = new JMenuItem("추가");
		addMenu.addActionListener(this);
		add(addMenu);
		
		delMenu = new JMenuItem("삭제");
		delMenu.addActionListener(this);
		add(delMenu);
		
		updateMenu = new JMenuItem("수정");
		updateMenu.addActionListener(this);
		add(updateMenu);
		
		listMenu = new JMenuItem("리스트");
		listMenu.addActionListener(this);
		add(listMenu);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == listMenu) {
			listMenuActionPerformed(e);
		}
		if (e.getSource() == updateMenu) {
			updateMenuActionPerformed(e);
		}
		if (e.getSource() == delMenu) {
			delMenuActionPerformed(e);
		}
		if (e.getSource() == addMenu) {
			addMenuActionPerformed(e);
		}
	}
	public void refreshList(AbsTableList list){
		Container contentPane = frame.getContentPane();
		contentPane.removeAll();
		contentPane.add(list);
		list.setTableModel();
		contentPane.validate();
	}
	
	public int searchNum(DaoTable daoTable, String msg, int profCode){
		ArrayList<String> lists;
		if(profCode==0){
			lists = daoTable.selectTableAllList();
		}else {
			lists =((DaoCourse)daoTable).selectTableAllListByCode(profCode);
		}
		 
		Object[] arlists = lists.toArray();
		int result = -1;
		try {
			String res = (String)JOptionPane.showInputDialog(null, msg+"하고자 하는 번호 선택", msg, JOptionPane.OK_CANCEL_OPTION,
					null, arlists, arlists[0]);
			StringTokenizer st = new StringTokenizer(res, ":");
			result = Integer.parseInt(st.nextToken().trim());
		} catch (NullPointerException e) {
		}
		return result;
	}
	
	protected abstract void addMenuActionPerformed(ActionEvent e);
	protected abstract void delMenuActionPerformed(ActionEvent e);
	protected abstract void updateMenuActionPerformed(ActionEvent e);
	protected abstract void listMenuActionPerformed(ActionEvent e);
	
}
